/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import server.MailServer;

/**
 *
 * @author leandro
 */
public class Server extends javax.swing.JFrame {

    /**
     * Creates new form Server
     */
    public Server() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtcrearservidor = new javax.swing.JButton();
        txtcrearusuario = new javax.swing.JButton();
        txteliminarusuario = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SERVIDOR");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("SERVIDOR");

        txtcrearservidor.setText("CREAR SERVIDOR");
        txtcrearservidor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcrearservidorActionPerformed(evt);
            }
        });

        txtcrearusuario.setText("CREAR USUARIO");
        txtcrearusuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtcrearusuarioActionPerformed(evt);
            }
        });

        txteliminarusuario.setText("ELIMINAR USUARIOS");
        txteliminarusuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txteliminarusuarioActionPerformed(evt);
            }
        });

        jMenu1.setText("Opciones");

        jMenuItem1.setText("Iniciar Sesion");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Salir");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtcrearusuario, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(txtcrearservidor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txteliminarusuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(txtcrearservidor)
                .addGap(18, 18, 18)
                .addComponent(txtcrearusuario)
                .addGap(18, 18, 18)
                .addComponent(txteliminarusuario)
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtcrearservidorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcrearservidorActionPerformed
        // TODO add your handling code here:
        MailServer server;
        String archivo= JOptionPane.showInputDialog(this, "Nombre de archivo");
        server=new MailServer(archivo);
        server.save();
        JOptionPane.showMessageDialog(this, "Servidor creado exitosamente");
    }//GEN-LAST:event_txtcrearservidorActionPerformed

    private void txtcrearusuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtcrearusuarioActionPerformed
        // TODO add your handling code here:
        
          try{
          JFileChooser elegir = new JFileChooser();
           elegir.setCurrentDirectory(new java.io.File("."));
           int opcion = elegir.showOpenDialog(this);
              if (opcion == JFileChooser.APPROVE_OPTION) {
                String pathArchivo = elegir.getSelectedFile().getPath();    
                FileInputStream fis = new FileInputStream(pathArchivo);
                String archivo = elegir.getSelectedFile().getName();
                ObjectInputStream ois = new ObjectInputStream(fis);
                MailServer s = (MailServer) ois.readObject();
                ois.close();
                String nombre=JOptionPane.showInputDialog(this, "Ingresar Usuario");
                s.addUsuario(nombre);
                s.save();        
                JOptionPane.showMessageDialog(this, "Usuario añadido exitosamente");             
              }
              }catch(FileNotFoundException e){
                    e.printStackTrace();
              }catch(IOException e){
                    e.printStackTrace();
              }catch(ClassNotFoundException e){
                    e.printStackTrace();
              }
    }//GEN-LAST:event_txtcrearusuarioActionPerformed

    private void txteliminarusuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txteliminarusuarioActionPerformed
        // TODO add your handling code here:
        try{
          JFileChooser elegir = new JFileChooser();
           elegir.setCurrentDirectory(new java.io.File("."));
           int opcion = elegir.showOpenDialog(this);
              if (opcion == JFileChooser.APPROVE_OPTION) {
                String pathArchivo = elegir.getSelectedFile().getPath();    
                FileInputStream fis = new FileInputStream(pathArchivo);
                String archivo = elegir.getSelectedFile().getName();
                ObjectInputStream ois = new ObjectInputStream(fis);
                MailServer s = (MailServer) ois.readObject();
                ois.close();
                DialogUsuarios dialog= new DialogUsuarios(this, true,s);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);
                            
              }
              }catch(FileNotFoundException e){
                    e.printStackTrace();
              }catch(IOException e){
                    e.printStackTrace();
              }catch(ClassNotFoundException e){
                    e.printStackTrace();
              }
        
        
    }//GEN-LAST:event_txteliminarusuarioActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        Index index= new Index();
        index.setLocationRelativeTo(null);
                index.setDefaultCloseOperation(index.EXIT_ON_CLOSE);
                index.setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Server.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Server s= new Server();
                s.setLocationRelativeTo(null);
                s.setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JButton txtcrearservidor;
    private javax.swing.JButton txtcrearusuario;
    private javax.swing.JButton txteliminarusuario;
    // End of variables declaration//GEN-END:variables
}
